package com.sap.apitest2.deprecated.service.depr;

import java.util.Collection;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.sap.apitest2.deprecated.process.CatalogProcess;
import com.sap.apitest2.deprecated.service.AbstractService;
import com.sap.apitest2.entity.Case;
import com.sap.apitest2.entity.Catalog;



@Path("/")
public class CatalogService extends AbstractService{
	
	protected CatalogProcess catalogProcess=new CatalogProcess();
	
	@Path(ACTION_CATALOGS+"/{id:\\d+}")//"/bpccatalogs/{id:\\d+}"
	@GET
	@Produces("application/json")
	public  Collection<Catalog> getCatalogs(@PathParam("id") long id){
		List<Catalog> catalogs=null;
		try{
			catalogs=catalogProcess.getEntities(Catalog.class.getName());
			System.out.println("get catalogs "+catalogs.size());
		}
		catch(Exception ex){
			this.LogException(ex);
		}
		return catalogs;
	}
	@Path(ACTION_CATALOG+"/{id:\\d+}")//"/bpccatalog/{id:\\d+}"
	@GET
	@Produces("application/json")
	public  Catalog getCatalog(@PathParam("id") long id){
		Catalog catalog=null;
		try{
			//org.datanucleus.api.jpa.PersistenceProviderImpl p=null;
			catalog=catalogProcess.getEntity(Catalog.class, id);
			//System.out.println("catalog "+catalog+"  "+id);
		}
		catch(Exception ex){
			this.LogException(ex);
		}
		return catalog;
	}
	@Path(ACTION_SAVECATALOG)//"/bpcsavecatalog"
	@POST
	@Consumes("application/json")
	public  Response  saveCatalog(Catalog entity){
		//System.out.println("input name "+entity.getName());
		try{
			System.out.println("input name "+entity.getName());
			catalogProcess.saveEntity(entity);
		}
		catch(Exception ex){
			this.LogException(ex);
			return Response.serverError().build();
		}
		Response resp= Response.ok().build();

		return resp;
	}
	
	@Path(ACTION_REMOVECATALOG+"/{id:\\d+}")//"/bpcremovecatalog/{id:\\d+}"
	@POST
	@Consumes("application/json")
	public  Response  removeCatalog(@PathParam("id") long id){
		try{
			Catalog entity=catalogProcess.getEntity(Catalog.class, id);
			
			System.out.println("remove id "+entity.getId());
			catalogProcess.removeEntity(entity);
		}
		catch(Exception ex){
			this.LogException(ex);
			return Response.serverError().build();
		}
		Response resp= Response.ok().build();
		return resp;
	}
	
	@Path(ACTION_CASES+"/{id:\\d+}")
	@GET
	@Produces("application/json")
	public  Collection<Case> getCases(@PathParam("id") long id){
		List<Case> Cases=null;
		try{
			Cases=catalogProcess.getEntities(Case.class.getName());
			System.out.println("get Cases "+Cases.size());
		}
		catch(Exception ex){
			this.LogException(ex);
		}
		return Cases;
	}
}
